//
//  LCAcitviceDetailViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/12.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCAcitviceDetailViewController.h"
#import "LCLoginViewController.h"
#import "LCLoginActiviceCell.h"
#import "LCTimeTableViewCell.h"
#import "LCWebTableViewCell.h"
#import "CountHeight.h"
#import "ActivityDetailModel.h"
static NSString *cellidentifier = @"LCWebTableViewCell";
static NSString *cellidentifier1 = @"LCTimeTableViewCell";
static NSString *cellidentifier2 = @"LCLoginActiviceCell";

@interface LCAcitviceDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>
{
    CGFloat webViewHeight;
}
@property (weak, nonatomic) IBOutlet UITableView    *tableView;
@property (strong, nonatomic) IBOutlet UIView       *headView;

@property (weak, nonatomic) IBOutlet UILabel        *summerLbl;//描述
@property (weak, nonatomic) IBOutlet UILabel        *activityType;//活动类型
@property (strong,nonatomic)ActivityDetailModel     *detailModel;
@property (strong, nonatomic) NSMutableDictionary   *dataDic;
@property (assign,nonatomic)BOOL                    receiveUp;
@end

@implementation LCAcitviceDetailViewController
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _receiveUp = YES;
    [self setBarTitle:@"活动详情"];
    self.rightImg.hidden = YES;
    self.rightBtn.hidden = YES;
    _dataDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [self loadCustomeView];
    [self loadData];
}
- (void)loadData
{
    [_dataDic setObject:self.event_id forKey:@"event_id"];
    [MDataProvider activityDetailBodyDic:_dataDic sucess:^(ActivityDetailModel *model)
    {
        _detailModel = model;
        _summerLbl.text = model.event_title;
        _activityType.text = model.event_type_name;
        [_tableView reloadData];
    } faile:^(NSString *str)
    {
        [SVProgressHUD showErrorWithStatus:str];
    }];
}
- (void)loadCustomeView
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _headView;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"LCWebTableViewCell" bundle:nil] forCellReuseIdentifier:cellidentifier];
    [_tableView registerNib:[UINib nibWithNibName:@"LCTimeTableViewCell" bundle:nil] forCellReuseIdentifier:cellidentifier1];
    [_tableView registerNib:[UINib nibWithNibName:@"LCLoginActiviceCell" bundle:nil] forCellReuseIdentifier:cellidentifier2];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadCartCount) name:nLogin object:nil];
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        LCWebTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
        cell1.webView.delegate = self;
        [cell1.webView loadHTMLString:_detailModel.event_content baseURL:nil];
        return cell1;
    }
    else if(indexPath.row == 1)
    {
        LCTimeTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellidentifier1];
        cell2.titleLbl3.hidden = YES;
        cell2.valueLbl.hidden = YES;
        if ([_state isEqualToString:@"未开始"])
        {
            cell2.titleLbl1.text = @"活动预计开始时间";
            cell2.timelbl1.text = _detailModel.expect_start;
            cell2.titleLbl2.text = @"活动预计结束时间";
            cell2.timeLbl2.text = _detailModel.expect_end;
        }
        else if([_state isEqualToString:@"报名中"])
        {
            cell2.titleLbl1.text = @"活动报名开始时间";
            cell2.timelbl1.text = _detailModel.signup_start;
            cell2.titleLbl2.text = @"活动报名结束时间";
            cell2.timeLbl2.text = _detailModel.signup_end;
        }
        else
        {
            cell2.titleLbl1.text = @"活动开始时间";
            cell2.timelbl1.text = _detailModel.event_start;
            cell2.titleLbl2.text = @"活动结束时间";
            cell2.timeLbl2.text = _detailModel.event_end;
        }
        return cell2;
    }
    else
    {
        LCLoginActiviceCell *cell3 = [tableView dequeueReusableCellWithIdentifier:cellidentifier2];
        if (get_sp(@"app_user"))
        {
            [cell3.loginBtn setTitle:@"报名" forState:UIControlStateNormal];
            cell3.lineView1.hidden = YES;
            cell3.lineView2.hidden = YES;
            cell3.titleLbl.hidden = YES;
        }
        else
        {
            [cell3.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
            cell3.lineView1.hidden = NO;
            cell3.lineView2.hidden = NO;
            cell3.titleLbl.hidden = NO;
        }
        __weak typeof(self) weak = self;
        [cell3 setLoginClickBlock:^{
            if (get_sp(@"app_user"))
            {
                if (weak.receiveUp)
                {
                    if (weak.detailModel.is_signup)
                    {
                        if (weak.detailModel.signupFlg)
                        {
                            [SVProgressHUD showInfoWithStatus:@"您已经报名过该活动"];
                        }
                        else
                        {
                            //报名
                            NSMutableDictionary  *signDic = [[NSMutableDictionary alloc]initWithCapacity:0];
                            [signDic setObject:get_sp(@"company_id") forKey:@"company_id"];
                            [signDic setObject:weak.event_id forKey:@"event_id"];
                            [MDataProvider activitySignupBodyDic:signDic sucess:^(NSString *resultStr)
                             {
                                 weak.receiveUp = NO;
                                 [SVProgressHUD showInfoWithStatus:resultStr];
                                 [self.navigationController popViewControllerAnimated:YES];
                             } faile:^(NSString *str)
                             {
                                 [SVProgressHUD showErrorWithStatus:str];
                             }];
                        }
                    }
                    else
                    {
                        ALERT_SHOW(@"该活动不能报名！");
                    }
                }
                else
                {
                    [SVProgressHUD showInfoWithStatus:@"不能重复报名"];
                }
                
            }
            else
            {
                //登录
                LCLoginViewController *vc = [[LCLoginViewController alloc]init];
                [self presentViewController:vc animated:YES completion:nil];
            }
        }];
            return cell3;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 300;
    }
    else if(indexPath.row == 1)
    {
        return 90;
    }
    else
    {
        if (get_sp(@"app_user"))
        {
            //报名
            return 110;
        }
        else
        {
          return 160;
        }
    }
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *script = [NSString stringWithFormat:
                        @"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var img;"
                        "var maxwidth=%f;"
                        "for(i=0;i <document.images.length;i++){"
                        "img = document.images[i];"
                        "if(img.width > maxwidth){"
                        "img.width = maxwidth;"
                        "}"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH - 36];
    [webView stringByEvaluatingJavaScriptFromString: script];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
}
//登录成功
-(void)loadCartCount
{
    [_tableView reloadData];
}
@end
